<template>
  <div>
    <div class="line-top"></div>
    <div v-show="list.length" class="container">
      <div class="store">
        <div class="store-left">故事</div>
        <div class="store-right">搜索故事</div>
      </div>
      <div class="list-control">
        <div class="list-control-filter">
          <span
            class="store-nav"
            :class="{on: item === filterBrand}"
            v-for="item in brands"
            :key="item"
            @click="handleFilterBrand(item)"
          >{{ item }}</span>
        </div>
      </div>
      <fourone v-for="item in filteredAndOrderedList" :info="item" :key="item.id"></fourone>
    </div>
  </div>
</template>




<script>
import fourone from "../components/fourone.vue";
export default {
  components: { fourone },
  computed: {
    list() {
      return this.$store.state.productList;
    },
    brands() {
      return this.$store.getters.brands;
    },
    colors() {
      return this.$store.getters.colors;
    },
    filteredAndOrderedList() {
      let list = [...this.list];
      // 按品牌过滤
      if (this.filterBrand !== "") {
        list = list.filter(item => item.brand === this.filterBrand);
      }
      // 按颜色过滤
      if (this.filterColor !== "") {
        list = list.filter(item => item.color === this.filterColor);
      }
      // 排序
      if (this.order !== "") {
        if (this.order === "sales") {
          list = list.sort((a, b) => b.sales - a.sales);
        } else if (this.order === "cost-desc") {
          list = list.sort((a, b) => b.cost - a.cost);
        } else if (this.order === "cost-asc") {
          list = list.sort((a, b) => a.cost - b.cost);
        }
      }
      return list;
    }
  },
  data() {
    return {
      filterBrand: "",
      filterColor: "",
      order: ""
    };
  },
  methods: {
    handleFilterBrand(brand) {
      if (this.filterBrand === brand) {
        this.filterBrand = "";
      } else {
        this.filterBrand = brand;
      }
    },
    handleFilterColor(color) {
      if (this.filterColor === color) {
        this.filterColor = "";
      } else {
        this.filterColor = color;
      }
    },
    handleOrderDefault() {
      this.order = "";
    },
    handleOrderSales() {
      this.order = "sales";
    },
    handleOrderCost() {
      if (this.order === "cost-desc") {
        this.order = "cost-asc";
      } else {
        this.order = "cost-desc";
      }
    }
  },
  mounted() {
    this.$store.dispatch("getProductList");
  }
};
</script>

<style>
.list-control {
  margin-left: 0px;
}
.list-control-filter {
  padding-bottom: 18px;
}
.store-nav {
  cursor: pointer;
  color: #484848;
  font-size: 14px;
  font-weight: 600;
  margin-right: 24px;
  margin-bottom: 10px;
}
.store-nav:hover {
  color: rgb(0, 132, 137);
}
.fourone-not-found {
  text-align: center;
  padding: 32px;
}
.store {
  width: 100%;
  height: 48px;
  /* padding: 8px; */
  overflow: hidden;
  line-height: 48px;
}
.store-left,
.store-right {
  display: inline-block;
  color: #484848 !important;
  /* margin-top: 24px; */
}
.store-left {
  font-size: 32px;
  font-weight: 800;
  /* margin-top: -24px; */
}
.store-right {
  float: right;
}
.line-top {
  width: 100%;
  height: 1px;
  background: #ebebeb;
  margin-bottom: 50px;
}
</style>